package com.mmc.controller;


import com.mmc.entity.UserBind;
import com.mmc.hfb.RequestHelper;
import com.mmc.result.R;
import com.mmc.service.IUserBindService;
import com.mmc.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/api/core/userBind")
public class UserBindController {

    @Autowired
    private IUserBindService userBindService;

    @PostMapping("/auth/bind")
    public R authBind(@RequestBody UserBind userBind, @RequestHeader("token")String token){
        Long userId = JwtUtils.getUserId(token);
        String formStr=userBindService.authBind(userBind,userId);

        return R.ok().data("formStr",formStr);
    }

    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        String sign = RequestHelper.getSign(map);
        if (!sign.equals(map.get("sign"))){
            return "fail";
        }
        userBindService.mmcNotify(map);
        return "success";
    }


}
